package com.mapproject.industrymap.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapproject.industrymap.entity.PersonalDataCatalogEntity;
import com.mapproject.industrymap.mapper.SysPersonalDataCatalogMapper;
import com.mapproject.industrymap.service.DataShareService;
import com.mapproject.industrymap.utils.ReturnValue;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DataShareServiceImpl extends ServiceImpl<SysPersonalDataCatalogMapper, PersonalDataCatalogEntity>  implements DataShareService {
    @Autowired
    SysPersonalDataCatalogMapper sysPersonalDataCatalogMapper;
    public ReturnValue sharePersonalData(String layername){
        QueryWrapper<PersonalDataCatalogEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper
                .eq("name",layername);
        List<Map<String, Object>> maps = sysPersonalDataCatalogMapper.selectMaps(queryWrapper);
        if (maps.size()>0){
            String name=maps.get(0).get("name").toString();
            String server_url=maps.get(0).get("server_url").toString();
            String layer_name=maps.get(0).get("layer_name").toString();
            String department_name=maps.get(0).get("department_name").toString();
            String layer_style=maps.get(0).get("layer_style").toString();
            //数据迁移至部门表中
            BasicDataSource basicDataSource = new BasicDataSource();
            basicDataSource.setDriverClassName("org.postgresql.Driver");
            basicDataSource.setUrl("jdbc:postgresql://localhost:5432/WJMap");
            basicDataSource.setUsername("postgres");
            basicDataSource.setPassword("lvshuangxi");
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            jdbcTemplate.setDataSource(basicDataSource);

            String insertToDepartmentSql = "INSERT INTO public.department_data_information(\n" +
                    "\tname, server_url, layer_name, department_name, layer_style)\n" +
                    "\tVALUES ('"+name+"', '"+server_url+"', '"+layer_name+"', '"+department_name+"', '"+layer_style+"');";
            jdbcTemplate.execute(insertToDepartmentSql);


            return new ReturnValue(200,"感谢您的共享！");
        }else{
            return new ReturnValue(200,"共享失败！");
        }




    }
    public ReturnValue delPersonalData(String layername){
        //数据迁移至部门表中
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("org.postgresql.Driver");
        basicDataSource.setUrl("jdbc:postgresql://localhost:5432/WJMap");
        basicDataSource.setUsername("postgres");
        basicDataSource.setPassword("lvshuangxi");
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(basicDataSource);

        String delPersonalSql = "DELETE FROM public.personal_data_information\n" +
                "\tWHERE name= '"+layername+"';";
        jdbcTemplate.execute(delPersonalSql);


        return new ReturnValue(200,"数据删除成功！");




    }

}

